#pragma once

#include <string>
#include <vector>
#include <stdlib.h>
class Util
{
public:
    // in 输入输出型参数，out 输出型参数，sep 分隔符
    static void StringSplit(std::string &in, std::vector<std::string> *out, const std::string &sep)
    {
        while (true)
        {
            auto pos = in.find(sep);
            if (pos == std::string::npos)
            {
                // 代表没有找到
                break;
            }
            std::string sub = in.substr(0, pos); //[)
            (*out).push_back(sub);
            in.erase(0, pos + 1); // 读取出的报文 从inbuffer中删 lk,..除
        }
    }
    static void ParsePackage(std::string &package, int *x, int *y, char *op)
    {
        //+-*/
        size_t pos = package.find("+");
        if (pos != std::string::npos)
        {
            // 找到了
            *op = '+';
            *x = atoi(package.substr(0, pos).c_str());
            *y = atoi(package.substr(pos + 1).c_str());
            return;
        }
        pos = package.find("-");
        if (pos != std::string::npos)
        {
            // 找到了
            *op = '-';
            *x = atoi(package.substr(0, pos).c_str());
            *y = atoi(package.substr(pos + 1).c_str());
            return;
        }
        pos = package.find("*");
        if (pos != std::string::npos)
        {
            // 找到了
            *op = '*';
            *x = atoi(package.substr(0, pos).c_str());
            *y = atoi(package.substr(pos + 1).c_str());
            return;
        }
        pos = package.find("/");
        if (pos != std::string::npos)
        {
            // 找到了
            *op = '/';
            *x = atoi(package.substr(0, pos).c_str());
            *y = atoi(package.substr(pos + 1).c_str());
            return;
        }
    }
    // 去除数据buffer中的空格
    static void RemoveSpace(std::string &in_buffer)
    {
        //size_t pos = 0;
        while (true)
        {
            size_t pos = in_buffer.find(" ");
            if (pos == std::string::npos)
            {
                // 没有找到
                return;
            }
            in_buffer.erase(pos, 1);
        }
    }
};
